package com.banshuibao.bsb_android.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.activity.AdoptActivity;
import com.banshuibao.bsb_android.activity.CommitOneOnActivity;
import com.banshuibao.bsb_android.activity.ConDetailsActivity;
import com.banshuibao.bsb_android.activity.NoAdoptActivity;
import com.banshuibao.bsb_android.adapter.ConsultationAdapter;
import com.banshuibao.bsb_android.adapter.UnAnswerAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.ConsutationModel;
import com.banshuibao.bsb_android.model.UnAnswerModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.LogUtil;
import com.banshuibao.bsb_android.util.XToast;

import java.util.List;

/**
 * Created by Administrator on 2017/12/22.
 */

public class UnAnsweredFragment extends Fragment implements AdapterView.OnItemClickListener {

    private TextView no_text;
    private PullToRefreshListView pullToRefreshListView;
    private ListView listView;
    private UnAnswerAdapter adapter;
    private boolean isUp = false;
    private int fristID = 1;
    private String role;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_unanswered, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        loadTwoData();
    }

    private void loadTwoData() {
        isUp = false;
        if (this.adapter != null) {
            int maxNum = 10;
            int count = adapter.getCount();
            if (count > 10) {
                maxNum += adapter.getCount() - 10;
            }
            fristID = 1;
            setPostTwo(fristID, maxNum, isUp);
        }
    }

    private void setPostTwo(int fristNum, int maxNum, final boolean isUp) {
        UnAnswerModel.body body = new UnAnswerModel.body();
        body.setSinceId(fristNum);
        body.setMaxId(maxNum);
        body.setType("0");
        body.setStatus("0");
        HttpUtil.doPost(Constants.Url.ASK_LIST, body, new HttpResponse(getActivity(), UnAnswerModel.class) {
            @Override
            public void onResult(Object obj) {
                UnAnswerModel s = (UnAnswerModel) obj;
//                LogUtil.e(s);
                int rows = s.getRows();
                List<UnAnswerModel.DataBean> data = s.getData();
                fristID += rows;
                if (isUp)
                    adapter.addRes(data);
                else
                    adapter.updateRes(data);
                if (rows == 0) {
                    if (adapter.getCount() == 0) {
                        no_text.setVisibility(View.VISIBLE);
                    } else {
                        no_text.setVisibility(View.GONE);
                    }
                    if (isUp)
                        XToast.showToast(getActivity(), "没有更多数据");
                } else {
                    no_text.setVisibility(View.GONE);
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullToRefreshListView.onRefreshComplete();
            }
        });
    }

    private void initView() {
        no_text = (TextView) getView().findViewById(R.id.msg_text);
        pullToRefreshListView = (PullToRefreshListView) getView().findViewById(R.id.fragment_unanswered_list);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadTwoData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                isUp = true;
                setPostTwo(fristID, fristID + 9, isUp);
            }
        });
        listView = pullToRefreshListView.getRefreshableView();
        listView.setDivider(null);
        adapter = new UnAnswerAdapter(getActivity(), null, R.layout.item_ask_answer);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        UnAnswerModel.DataBean item = adapter.getItem(i - 1);
        Intent intent = new Intent(getActivity(), CommitOneOnActivity.class);
        intent.putExtra("item", item);
        startActivity(intent);
    }
}
